package com.bawei.image.loader

import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.bawei.image.IImageLoader
import com.bawei.image.ImageOptions
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import java.io.File
import java.lang.ref.SoftReference

/**
 * Author: Jason Zhang
 * Date: 2024/9/12
 * Desc: Glide图片加载器
 */
class GlideImageLoader: IImageLoader {

    override fun loadFileIntoImageView(context: Context, file: File, target: ImageView, options: ImageOptions) {
        loadImage(context, file, target, options)
    }

    override fun loadUrlIntoImageView(context: Context, url: String, target: ImageView, options: ImageOptions) {
        loadImage(context, url, target, options)
    }

    override fun loadResourceIntoImageView(context: Context, resource: Int, target: ImageView, options: ImageOptions) {
        loadImage(context, resource, target, options)
    }

    override fun loadByteArrayIntoImageView(context: Context, bytes: ByteArray, target: ImageView, options: ImageOptions) {
        loadImage(context, bytes, target, options)
    }

    override fun loadUriIntoImageView(context: Context, uri: Uri, target: ImageView, options: ImageOptions) {
        loadImage(context, uri, target, options)
    }


    private fun loadImage(context: Context, source: Any, target: ImageView, options: ImageOptions){
        var softRef = SoftReference(context)
        softRef.get()?.run {
            var requestManager = Glide.with(this)

            //是否加载Gif图片
            if (options.isGifImage){
                requestManager.asGif()
            }

            //获取请求Builder
            var requestBuilder = requestManager.load(source)

            //加载占位图
            if (options.placeImage > 0){
                requestBuilder.placeholder(options.placeImage)
            }

            //加载错误时占位图
            if (options.errorImage > 0){
                requestBuilder.error(options.errorImage)
            }

            //是否使用内存缓存
            if (options.skipMemoryCache){
                requestBuilder.skipMemoryCache(true)
            }

            //是否使用磁盘缓存
            if (options.skipDiskCache){
                requestBuilder.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            }else{
                requestBuilder.diskCacheStrategy(DiskCacheStrategy.NONE)
            }

            //设置图片大小
            if (options.imgSize.size == 2){
                requestBuilder.submit(options.imgSize[0], options.imgSize[1])
            }

            //设置圆形图片
            if (options.isCircle){
                requestBuilder.transform(CenterCrop(), CircleCrop())
            } else {
                requestBuilder.transform(CenterCrop())
            }

            //设置圆角
            if (options.roundRadius > 0){
                requestBuilder.transform(CenterCrop(), RoundedCorners(options.roundRadius))
            }

            requestBuilder.into(target)

        }
    }


}